package com.link.base.base.report.service;

import com.link.base.base.user.model.User;
import com.link.base.base.report.dao.mybatis.mapper.GuideReportMapper;
import com.link.base.base.report.model.GuideReport;
import com.link.base.base.report.model.SalesAmount;
import com.link.base.base.staticvalue.dao.mybatis.mapper.StaticvalueMapper;
import com.link.base.base.staticvalue.model.Staticvalue;
import com.link.base.base.user.dao.mybatis.mapper.UserMapper;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.dao.siebel.mapper.BasicMapperSiebel;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 商机报表
 */
@Service
public class GuideReportServiceImpl extends BasicServiceImpl<GuideReport>
        implements GuideReportService {

    @Resource
    private GuideReportMapper guideReportMapper;
    @Resource
    private StaticvalueMapper staticvalueMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public BasicMapper<GuideReport> getBasicMapper() {
        return guideReportMapper;
    }

    @Override
    public BasicMapperSiebel<GuideReport> getSiebelBasicMapper()
            throws Exception {

        return null;
    }

    /**
     * 逻辑是展示当前登陆人所创建的所有商机所处的各阶段汇总。
     * 就是商机上现在有个字段叫当前阶段，按照商机所处的阶段对商机数进行汇总。
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryOptyStageNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryOptyStageNum(qr);
    }

    /**
     * 一周商机数量汇总表：逻辑是当前登陆人创建的今日以前6天一直到今天每天新建的商机数
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryNewOptyNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryNewOptyNum(qr);
    }

    /**
     * 查询客户状态数量汇总
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccntStatusNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccntStatusNum(qr);
    }

    /**
     * 客户关键节点推进报表
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccntKeyNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccntKeyNum(qr);
    }

    /**
     * 产品销售分析（销售额）
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryProOrderSalesMoney(GuideReport qr) throws Exception {
        return guideReportMapper.queryProOrderSalesMoney(qr);
    }

    /**
     * 产品销售分析（订单数量）
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryProOrderSalesNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryProOrderSalesNum(qr);
    }

    /**
     * 销售订单状态分析
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> querySalesOrderStatusNum(GuideReport qr) throws Exception {
        return guideReportMapper.querySalesOrderStatusNum(qr);
    }

    /**
     * 企点客服接待统计
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccountPeopleNum(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccountPeopleNum(qr);
    }

    /**
     * 商机预算top10
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryOptyBudgtTop10(GuideReport qr) throws Exception {
        return guideReportMapper.queryOptyBudgtTop10(qr);
    }

    /**
     * 产品收藏top10
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryProductCollTop10(GuideReport qr) throws Exception {
        return guideReportMapper.queryProductCollTop10(qr);
    }

    /**
     * 本周销量情况
     *
     * @param qr
     * @return
     */
    @Override
    public List<SalesAmount> querySalesAmountReport(GuideReport qr) throws Exception {
        return guideReportMapper.querySalesAmountReport(qr);
    }

    /**
     * 标签组信息
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccnttagReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccnttagReport(qr);
    }

    /**
     * 标签组详情信息
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccnttagItemReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccnttagItemReport(qr);
    }

    /**
     * 客户类型分析
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryAccntTypeReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryAccntTypeReport(qr);
    }

    /**
     * 本周客户连接总结
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryContonectAccntReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryContonectAccntReport(qr);
    }

    /**
     * 本周商机汇总分析
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryOptyStudyReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryOptyStudyReport(qr);
    }

    /**
     * 本周订单数量分析
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> queryOrderNumReport(GuideReport qr) throws Exception {
        return guideReportMapper.queryOrderNumReport(qr);
    }

    /**
     * 销售产品TOP10
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> querySalesProductTop10Report(GuideReport qr) throws Exception {
        return guideReportMapper.querySalesProductTop10Report(qr);
    }

    /**
     * 品牌维度查询销售额
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> querySaleAmountByBrand(GuideReport qr) throws Exception {
        Staticvalue stageType = new Staticvalue();
        stageType.setType("PROD_BRAND");
        stageType.setActiveFlag("Y");
        List<Staticvalue> brandList = staticvalueMapper.queryByLovType(stageType);
        List<GuideReport> list = guideReportMapper.querySaleAmountByBrand(qr);
        List<GuideReport> result = new ArrayList<GuideReport>();
        for (Staticvalue staticvalue : brandList) {
            GuideReport object = new GuideReport();
            object.setName(staticvalue.getName());
            Boolean hasFlag = false;
            for (GuideReport item : list) {
                if (item.getName().equals(staticvalue.getName())) {
                    object.setValue(item.getValue());
                    hasFlag = true;
                    break;
                }
            }
            if (!hasFlag) {
                object.setValue("0");
            }
            result.add(object);
        }
        return result;
    }

    /**
     * 导购维度查询销售额
     *
     * @param qr
     * @return
     */
    @Override
    public List<GuideReport> querySaleAmountByGuide(GuideReport qr) throws Exception {
        User user = new User();
        user.setOrgId(Long.parseLong(qr.getFilterParm2()));
        List<User> userList = userMapper.queryUserList(user);
        List<GuideReport> list = guideReportMapper.querySaleAmountByGuide(qr);
        List<GuideReport> result = new ArrayList<GuideReport>();
        for (User emp : userList) {
            GuideReport object = new GuideReport();
            object.setName(emp.getFirstName());
            Boolean hasFlag = false;
            for (GuideReport item : list) {
                if (item.getId().equals(emp.getId())) {
                    object.setValue(item.getValue());
                    hasFlag = true;
                    break;
                }
            }
            if (!hasFlag) {
                object.setValue("0");
            }
            result.add(object);
        }
        return result;
    }

    /**
     * 查询上周导购通过导购助手接待的客户数量，销售的产品种类及销售额最高的产品
     *
     * @param qr
     * @return
     */
    @Override
    public GuideReport queryAccntTypeNumLastWeek(GuideReport qr) throws Exception {
        if (StringUtils.isBlank(qr.getFilterType())) {
            qr.setFilterType("lastweek");
        }
        return guideReportMapper.queryAccntTypeNum(qr);
    }

    /**
     * 统计每周销售额（用于销售周报）
     *
     * @param qr
     * @return
     */
    @Override
    public List<SalesAmount> querySalesAmountWeekReport(GuideReport qr) {
        return guideReportMapper.querySalesAmountWeekReport(qr);
    }

}
